import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/Helper.dart';
import '../../model/xhsskd.dart';
import '../../pages/xhss/xhssHelper.dart';
import '../../pages/xhss/xhss_count_panel.dart';
import '../../widgets/someCards.dart';

import '../../model/xhss.dart';

class XHSSCountPage extends StatefulWidget {
  @override
  @override
  createState() => new XHSSCountPageState();
}

class XHSSCountPageState extends State<XHSSCountPage> {
  late DateTime t1;
  late DateTime t2;
  late List<XHSSKD> RList;
  bool IfOK = false;

  final TextEditingController c2 = new TextEditingController();

  @override
  void initState() {
    super.initState();
    //s1 = widget.s1;
    //UIData.CurrentUser!.Authority="水调值班员,电厂值班员";
    t1 = DateTime(2020, 1, 1);
    t2 = DateTime(2021, 1, 1);
    loadData();
  }

  loadData() async {
    var xx = await XHSSKD.GetList(
        "TZTM>=@T1 and TZTM<=@T2",
        {"T1": Helper.DateTimeToString(t1), "T2": Helper.DateTimeToString(t2)},
        0,
        50);

    setState(() {
      RList = xx;
      IfOK = true;
    });
  }

  Widget getDateTime1(BuildContext context) {
    return ElevatedButton(
        onPressed: () {
          DatePicker.showDatePicker(context,
              // 是否展示顶部操作按钮
              showTitleActions: true,
              // change事件

              // 确定事件
              onConfirm: (date) {
            print('confirm $date');
            setState(() {
              t1 = date;
            });
            loadData();
          },
              // 当前时间
              currentTime: DateTime.now(),
              // 语言
              locale: LocaleType.zh);
        },
        child: Text(
          t1 == null ? '请选择' : Helper.DateTimeToString(t1),
          style: TextStyle(color: Colors.blue),
        ));
  }

  Widget getDateTime2(BuildContext context) {
    return ElevatedButton(
        onPressed: () {
          DatePicker.showDatePicker(context,
              // 是否展示顶部操作按钮
              showTitleActions: true,
              // change事件

              // 确定事件
              onConfirm: (date) {
            print('confirm $date');
            setState(() {
              t2 = date;
            });
            loadData();
          },
              // 当前时间
              currentTime: DateTime.now(),
              // 语言
              locale: LocaleType.zh);
        },
        child: Text(
          t2 == null ? '请选择' : Helper.DateTimeToString(t2),
          style: TextStyle(color: Colors.blue),
        ));
  }

  @override
  Widget build(BuildContext context) {
    // if (s1 == null) return Center(child:SpinKitCircle(color: Colors.blue,));

    // TODO: implement build
    return Scaffold(
        appBar: AppBar(title: Text('泄洪设施运行统计')),
        //bottomSheet:bottomPanel2() ,

        body: IfOK == false
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : SingleChildScrollView(
                child: new Container(
                    margin: EdgeInsets.only(top: 8.0),
                    child: new Column(children: <Widget>[
                      SomeCard.inCard(ListTile(
                        title: Text("开始日期"),
                        trailing: getDateTime1(context),
                      )),
                      SomeCard.inCard(ListTile(
                        title: Text("结束日期"),
                        trailing: getDateTime2(context),
                      )),
                      XHSSCountPanel(RList),
                    ])),
              ));
  }
}
